Dashboard#

# Import libraries needed for this assignment
from hashlib import sha1
import altair as alt
import pandas as pd
from vega_datasets import data
import numpy as np
alt.data_transformers.enable("default", max_rows=None)
# Importing the data
url='https://raw.githubusercontent.com/UBC-MDS/exploratory-data-viz/main/data/street_trees.csv'
street_trees_df = pd.read_csv(url, sep=';')
# Filtered dataframe that includes relevant columns and rows as well as newly created columns
trees_df=street_trees_df.drop(['TREE_ID', 'CIVIC_NUMBER', 'STD_STREET','CULTIVAR_NAME','ASSIGNED','ROOT_BARRIER','PLANT_AREA','ON_STREET_BLOCK','ON_STREET','STREET_SIDE_NAME','CURB','Geom'], axis=1)
trees_df = trees_df[trees_df['GENUS_NAME'].isin(['ACER', 'PRUNUS'])]
trees_df=trees_df.assign(YEAR_PLANTED=np.where(trees_df.DATE_PLANTED=='Nat','N/A', trees_df['DATE_PLANTED'].str[:4]))
trees_df.loc[:,'TREE_TYPE']=np.where((trees_df.COMMON_NAME.str.contains('MAPLE')), 'MAPLE',np.where((trees_df.COMMON_NAME.str.contains('CHERRY')), 'CHERRY',
                            np.where((trees_df.COMMON_NAME.str.contains('PLUM')),'PLUM','OTHER')))
no_nulls_df = pd.notnull(trees_df['YEAR_PLANTED'])
no_nulls_df=trees_df[no_nulls_df]
select_tree_type = alt.selection_multi(fields=['TREE_TYPE'],bind='legend')

trees_planted_chart = alt.Chart(trees_df).mark_bar().encode(
    alt.X('count()', title='Tree Count'), 
    alt.Y('TREE_TYPE', sort='x', title=None),
    alt.Color('TREE_TYPE', title='Tree Type'),
    opacity=alt.condition(select_tree_type, alt.value(1.0),alt.value(0.2)),
    tooltip=[alt.Tooltip('GENUS_NAME', title='Genus'), alt.Tooltip('TREE_TYPE', title='Tree Type'), alt.Tooltip('count()', title='Count')]
).add_selection(select_tree_type).properties(title='The Number of Trees Planted by Tree Type',width=600)
select_tree_type = alt.selection_multi(fields=['TREE_TYPE'],bind='legend')

planted_trees_by_year_chart=alt.Chart(no_nulls_df).mark_line().encode(
    alt.X('YEAR_PLANTED'), 
    alt.Y('count()'),
    color='TREE_TYPE').properties(title='Trends in Trees Planted: 1998-2019',width=500, height=400)

planted_trees_by_year_pt_chart=alt.Chart(no_nulls_df).mark_point().encode(
    alt.X('YEAR_PLANTED', title='Year'), 
    alt.Y('count()', title='Tree Count'),
    alt.Color('TREE_TYPE', title = 'Tree Type'),
    tooltip=[alt.Tooltip('GENUS_NAME', title='Genus'), alt.Tooltip('TREE_TYPE', title='Tree Type'), alt.Tooltip('count()', title='Count')]
    ).properties(width=500, height=400)
select_tree_type = alt.selection_multi(fields=['TREE_TYPE'],bind='legend')

height_title = alt.TitleParams(
    "Height Range of Planted Trees: 1998-2019",
     subtitle = "0-10 for every 10 feet (e.g., 0 = 0-10 ft, 1 = 10-20 ft, 2 = 20-30 ft, and10 = 100+ ft)")

height_chart=alt.Chart(trees_df, title=height_title).mark_bar().encode(
    alt.X('HEIGHT_RANGE_ID',title="Height (ft)"), 
    alt.Y('count()', sort='x', title='Tree Count'),
    alt.Color('TREE_TYPE', legend=None),
    opacity=alt.condition(select_tree_type, alt.value(0.8),alt.value(0.1)), 
    tooltip=[alt.Tooltip('GENUS_NAME', title='Genus'), alt.Tooltip('TREE_TYPE', title='Tree Type'), alt.Tooltip('count()', title='Count')]
).add_selection(select_tree_type).properties(width=500, height=400)


diameter_title = alt.TitleParams(
    "Diameter (DBH) of Planted Trees",
     subtitle = "DBH in inches (DBH stands for diameter of tree at breast height)")

diameter_chart=alt.Chart(trees_df, title=diameter_title).mark_point(clip=True).encode(
    alt.X('DIAMETER',title="Diameter (in)", scale=alt.Scale(domain=[0,80])), 
    alt.Y('count()', title='Tree Count', scale=alt.Scale(type='symlog'), sort='x'),
    alt.Color('TREE_TYPE' ),
    opacity=alt.condition(select_tree_type, alt.value(0.8),alt.value(0.1)),
    tooltip=[alt.Tooltip('GENUS_NAME', title='Genus'), alt.Tooltip('TREE_TYPE', title='Tree Type'), alt.Tooltip('count()', title='Count')],
    shape='TREE_TYPE'
).add_selection(select_tree_type).properties(width=500, height=400).interactive()
tree_types = sorted(trees_df['TREE_TYPE'].unique())
dropdown = alt.binding_select(name='Tree Type ', options=tree_types)
select_trees = alt.selection_single(fields=['TREE_TYPE'], bind=dropdown)

neighbourhood_chart = alt.Chart(trees_df).mark_bar().encode(
    alt.X('count()', title='Tree Count'), 
    alt.Y('NEIGHBOURHOOD_NAME', sort='x', title=None),
    alt.Color('TREE_TYPE'),
    opacity=alt.condition(select_trees, alt.value(0.8),alt.value(0.2)),
    tooltip=[alt.Tooltip('GENUS_NAME', title='Genus'), alt.Tooltip('TREE_TYPE', title='Tree Type'), alt.Tooltip('count()', title='Count')]
).properties(title='Trees Planted by Neighbourhood',width=500, height=400).add_selection(select_trees)
neighbourhood_chart = (neighbourhood_chart.properties(title='Trees Planted by Neighbourhood',height=250, width=400))
planted_trees_by_year_chart = (planted_trees_by_year_chart.properties(title='Trends in Trees Planted: 1998-2019',height=250, width=400))
planted_trees_by_year_pt_chart = (planted_trees_by_year_pt_chart.properties(title='Trends in Trees Planted: 1998-2019',height=250, width=400))
height_chart = (height_chart.properties(title=height_title,height=250, width=400))
diameter_chart = (diameter_chart.properties(title=diameter_title,height=250, width=400))
dashboard_title = alt.TitleParams("City of Vancouver Tree Inventory",subtitle = "1989-2019", anchor='middle')


(trees_planted_chart & ((planted_trees_by_year_chart + planted_trees_by_year_pt_chart)| diameter_chart)& (neighbourhood_chart| height_chart )).properties(title=dashboard_title
).resolve_scale(
    color='independent',
    shape='independent')